﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Resources;
using System.Collections;

namespace Kokomo.PeInspector.Reflection
{
	class AssemblyPackResource : AssemblyResource
	{
		public AssemblyResource Container { get; private set; }
		public string Key { get; private set; }

		public AssemblyPackResource(
			AssemblyResource container,
			string key
			)
			: base(key, container.Assembly)
		{
			if (container == null) throw new ArgumentNullException("container");
			if (key == null) throw new ArgumentNullException("key");

			this.Container = container;
			this.Key = key;
		}

		public override System.IO.Stream GetResourceStream()
		{
			Stream parentResourceStream = this.Container.GetResourceStream();

			ResourceReader reader = new ResourceReader(parentResourceStream);
			Stream resourceStream = null;
			foreach (DictionaryEntry entry in reader)
			{
				if (object.Equals(entry.Key, this.Key))
				{
					resourceStream = entry.Value as Stream;
					break;
				}
			}

			return resourceStream;
		}

		public override int DetermineSize()
		{
			return this.GetResourceData().Length;
		}
	}
}
